package com.ciwong.epaper.modules.reciteWords.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.reciteWords.adapter.FrgUnlearnAdapter;
import com.ciwong.epaper.modules.reciteWords.bean.UnLearnWordBean;
import com.ciwong.epaper.modules.reciteWords.bean.UnLearnedWordItemBean;
import com.ciwong.epaper.modules.reciteWords.dao.WordDao;
import com.ciwong.epaper.modules.reciteWords.ui.ReciteWordLisActivity;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.widget.PullToRefreshView;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class TabUnlearnFragment extends BaseFragment implements PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {
    private String TAG = "TabUnlearnFragment";
    private LinearLayout ll_empty;
    private View cv_unlearn_fragment;
    private RecyclerView rv_fragment_unlearn;
    private PullToRefreshView refresh_recite_word_unlearn;
    private FrgUnlearnAdapter frgUnlearnAdapter;
    private List<UnLearnedWordItemBean> list = new ArrayList<>();
    private int DEFAULT_PAGER_INDEX = 1;//请求第几页
    private int PAGER_INDEX = DEFAULT_PAGER_INDEX;//请求第几页
    private int PAGER_SIZE = 20;//每页请求数量
    private boolean headRefresh;
    private int count = 0;
    private boolean flagFirst = true;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_word_list_unlearn, container, false);
        CWLog.i(TAG, "onCreateView");
        return initView(view);
    }

    private View initView(View view) {
        ll_empty = view.findViewById(R.id.ll_empty);
        cv_unlearn_fragment = view.findViewById(R.id.cv_unlearn_fragment);
        rv_fragment_unlearn = view.findViewById(R.id.rv_fragment_unlearn);
        refresh_recite_word_unlearn = view.findViewById(R.id.refresh_recite_word_unlearn);
        refresh_recite_word_unlearn.setOnHeaderRefreshListener(this);
        refresh_recite_word_unlearn.setOnFooterRefreshListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        CWLog.i(TAG, "onActivityCreated");
        initEvent();
//        initData();
    }

    public void initData() {
        if (flagFirst) {
            flagFirst = !flagFirst;
            getNetData(PAGER_INDEX, PAGER_SIZE);
        }
    }

    private void initEvent() {
//        handleView(list, count);
        frgUnlearnAdapter = new FrgUnlearnAdapter(getContext());
        rv_fragment_unlearn.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_fragment_unlearn.setAdapter(frgUnlearnAdapter);

    }

    private void handleView(List<UnLearnedWordItemBean> list, int count) {
        CWLog.i(TAG, " initData 单词数量：" + count);
        //初始化时处理单词数量
        ReciteWordLisActivity activity = (ReciteWordLisActivity) getActivity();
        if (activity != null)
            activity.showWordCount(count);
        if (list != null && list.size() > 0) {
            ll_empty.setVisibility(View.GONE);
            cv_unlearn_fragment.setVisibility(View.VISIBLE);
        } else {

            ll_empty.setVisibility(View.VISIBLE);
            cv_unlearn_fragment.setVisibility(View.GONE);
        }
    }

    public int getWordNum() {
//        CWLog.i(TAG, " getWordNum 单词数量：" + list.size());
        return count;
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        headRefresh = true;
        PAGER_INDEX = DEFAULT_PAGER_INDEX;
        getNetData(PAGER_INDEX, PAGER_SIZE);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        headRefresh = false;
        PAGER_INDEX++;
        getNetData(PAGER_INDEX, PAGER_SIZE);
    }

    private void refreshComplete(boolean isHeadRefresh) {
        if (isHeadRefresh) {
            refresh_recite_word_unlearn.onHeaderRefreshComplete();
        } else {
            refresh_recite_word_unlearn.onFooterRefreshComplete();
        }
    }

    public void getNetData(int pageIndex, int pagerSize) {
        if (!NetworkUtils.isOnline()) {
            refreshComplete(headRefresh);
            ToastUtil.INSTANCE.toastCenterNoNetError();
            return;
        }
        int userId = (int) EApplication.getInstance().getUserInfoBase().getUserId();
        if (userId <= 0) return;
        showCricleProgress(null);
        WordDao.getInstance().getUnlearnedWords(userId, pageIndex, pagerSize, new BaseExtCallBack(getContext()) {
            @Override
            public void success(Object data) {
                refreshComplete(headRefresh);
                hideCricleProgress();
                CWLog.d(TAG, " success getLearnedWords : " + data.toString());
                UnLearnWordBean bean = (UnLearnWordBean) data;
                List<UnLearnedWordItemBean> unLearnedList = bean.getList();
                if (null != unLearnedList && unLearnedList.size() <= 0) {
                    refresh_recite_word_unlearn.setFooterState(PullToRefreshView.PULL_TO_NO_MORE);
                    return;
                }
                if (headRefresh) list.clear();//刷新时先清空集合
                list.addAll(unLearnedList);
                count = bean.getCount();
                handleView(list, bean.getCount());
                frgUnlearnAdapter.setData(list);
            }

            @Override
            public void failed(int errorCode, Object data) {
                refreshComplete(headRefresh);
                hideCricleProgress();
                CWLog.d(TAG, "failed getLearnedWords : " + data.toString());
                super.failed(errorCode, data);
            }

            @Override
            public void failed(Object data) {
                failed(-1, data);
            }
        });

    }

}
